﻿using UnityEngine;
using System.Collections;

[AddComponentMenu("UITools/Loading")]
public class UILoading : MonoBehaviour
{
	static public UILoading Instance;
	
	void Awake () {
		if (Instance == null)
		{
			Instance = this; 

			NetworkDispatcher.Instance.OnSendRequestToServer += ShowLoading;
			NetworkDispatcher.Instance.OnRecieveRequestFromServer += HideLoading;
			NetworkDispatcher.Instance.OnReconnectToServer += ShowLoading;
			NetworkDispatcher.Instance.OnReconnectToServerFinish += OnReconnectToServerFinish;
		}
	}

	void OnDestroy () 
	{ 
		if (Instance == this)
		{
			Instance = null;
			
			NetworkDispatcher.Instance.OnSendRequestToServer -= ShowLoading;
			NetworkDispatcher.Instance.OnRecieveRequestFromServer -= HideLoading;
			NetworkDispatcher.Instance.OnReconnectToServer -= ShowLoading;
			NetworkDispatcher.Instance.OnReconnectToServerFinish -= OnReconnectToServerFinish;
		}
	}

	public float ConnectTime = 20.0f;
	float mRemainTime = 0.0f;
	
	// async loading
	public AsyncOperation mAsyncLoading = null;
	public UISlider SldLoading;
	public GameObject LoadObject;
	public GameObject ObjLoadBackground;
	
	public GameObject NetMsgObject;
	public GameObject ObjNetReconnect;

	string loadingScene = "";
	string mLastScene = "";
	
	// Use this for initialization
	void Start ()
	{
		//DontDestroyOnLoad(gameObject);
		

		
		Show(false);
		
		if (LoadObject != null)
			LoadObject.SetActive(false);
	}
	
	// Update is called once per frame
	void Update ()
	{


		if (NetMsgObject != null && NetMsgObject.activeInHierarchy)
		{
			if (mRemainTime > 0.0f)
			{
				mRemainTime -= Time.deltaTime;
				if (mRemainTime <= 0.0f)
				{
					NetworkDispatcher.Instance.DisconnectGameServer();

					if (ObjNetReconnect != null)
					{
						ObjNetReconnect.SetActive(true);
					}
					else
					{
						UIShowMsgError.Instance.OnShowErrorInfo((uint)MSG_ERROR.CONNECT_SERVER_FAILD);
					}
					
					Show(false);
				}
			}
		}
		
		if (mAsyncLoading != null)
		{
			if (!mAsyncLoading.isDone)
			{
				if (LoadObject != null && !LoadObject.activeInHierarchy)
					LoadObject.SetActive(true);
				
				if (SldLoading != null)
				{
					SldLoading.sliderValue = mAsyncLoading.progress;
				}

				if (ObjLoadBackground != null)
				{
					if (!ObjLoadBackground.activeInHierarchy)	ObjLoadBackground.SetActive(true);
				}
			}
			else
			{				
				if (LoadObject != null && LoadObject.activeInHierarchy)
					LoadObject.SetActive(false);
			}
		}
	}
	
	public bool IsShow()
	{
		if (NetMsgObject != null)
			return NetMsgObject.activeInHierarchy;
		
		return false;
	}
	
	public void Show(bool enable)
	{
		if (NetMsgObject != null)
			NetMsgObject.SetActive(enable);
		
		if (enable && ConnectTime > 0)
		{
			mRemainTime = ConnectTime;
		}
		else
		{
			mRemainTime = 0.0f;
		}
	}
	
	public void ShowLoading()
	{
		UILoading.Instance.Show(true);
	}
	
	public void HideLoading()
	{
		UILoading.Instance.Show(false);
		
		//NetworkDispatcher.Instance.DisconnectGameServer();
	}
	
	void OnReconnectToServerFinish()
	{
		UILoading.Instance.Show(false);
	}
	
	public IEnumerator Load(string name)
	{	
		mAsyncLoading = Application.LoadLevelAsync(name);
        yield return mAsyncLoading;
	}
	
	public void LoadScene(string name, bool isAsync)
	{
		mLastScene = Application.loadedLevelName;

		loadingScene = "";
		if (!isAsync || Application.loadedLevelName.Equals(name))
		{
			Application.LoadLevel(name);
		}
		else
		{
			StartCoroutine(Load(name));
			loadingScene = name;
		}
	}
	
	public bool IsLoadingComplete()
	{
		if (mAsyncLoading != null && !mAsyncLoading.isDone)
			return false;
		
		return true;
	}

	void Reconnect()
	{
		//NetworkDispatcher.Instance.ReconnectGameServer();

		if (ObjNetReconnect != null)	ObjNetReconnect.SetActive(false);
	}
}

